//
//  JMCouponTableViewCell.m
//  SYPerfectDemo
//
//  Created by Lssyyy on 2019/7/19.
//  Copyright © 2019年 Lssyyy. All rights reserved.
//

#import "JMCouponTableViewCell.h"
#define disabledColor UIColorWithRGB16Radix(0xB2B2B2)
#define ableColor UIColorWithRGB16Radix(0xFF991F)
@interface JMCouponTableViewCell()
@property (nonatomic,strong) UIView *backView;
@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UILabel *limitLabel;//使用条件
@property (nonatomic,strong) UILabel *timeLabel;//有效期
@end

@implementation JMCouponTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self layoutUI];
    }
    return self;
}

- (void)layoutUI{
    self.backView = [UIView new];
    self.backView.layer.borderWidth = 1.0;
    [self.contentView addSubview:self.backView];
    
    self.titleLabel = [UILabel new];
    self.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.backView addSubview:self.titleLabel];
    
    self.limitLabel = [UILabel new];
    self.limitLabel.font = [UIFont systemFontOfSize:13];
    self.limitLabel.text = @"满xxx可用";
    [self.backView addSubview:self.limitLabel];
    
    self.timeLabel = [UILabel new];
    self.timeLabel.font = [UIFont systemFontOfSize:13];
    self.timeLabel.text = @"有效期：-------";
    [self.backView addSubview:self.timeLabel];
    
    UIView *cv = self.contentView;
    
    self.titleLabel.sd_layout
    .leftSpaceToView(self.backView, 15)
    .topSpaceToView(self.backView, 15)
    .rightSpaceToView(self.backView, 15)
    .autoHeightRatio(0);
    
    self.limitLabel.sd_layout
    .leftEqualToView(self.titleLabel)
    .topSpaceToView(self.titleLabel, 10)
    .rightSpaceToView(self.backView, 15)
    .autoHeightRatio(0);
    
    self.timeLabel.sd_layout
    .leftEqualToView(self.titleLabel)
    .topSpaceToView(self.limitLabel, 10)
    .rightSpaceToView(self.backView, 15)
    .autoHeightRatio(0);
    
    self.backView.sd_layout
    .leftSpaceToView(cv,10)
    .rightSpaceToView(cv, 10)
    .topSpaceToView(cv,1);
    self.backView.sd_cornerRadius = @(5);
    

    [self.backView setupAutoHeightWithBottomView:self.timeLabel bottomMargin:15];
    
    [self setupAutoHeightWithBottomView:self.backView bottomMargin:13];
}
- (void)setTitleStr:(NSString *)titleStr{
//    _titleStr = titleStr;
//    self.titleLabel.text = titleStr;
    
    NSString *changeStr = [NSString stringWithFormat:@"%@店铺优惠券",titleStr];
    
    NSMutableAttributedString *text  = [self changeToAttributeStringWithSubstringArray:@[titleStr] Color:self.titleLabel.textColor FontSize:22 str:changeStr];
    self.titleLabel.attributedText = text;
}
- (void)configCell:(id)data{
 
}

- (void)setCellIndexPath:(NSIndexPath *)cellIndexPath{
    _cellIndexPath = cellIndexPath;
    if (_cellIndexPath.section != 0) {
        self.backView.backgroundColor = UIColorWithRGB16Radix(0xF3F3F3);
        self.backView.layer.borderColor = UIColorWithRGB16Radix(0xE2E2E2).CGColor;
        self.titleLabel.textColor = disabledColor;
        self.limitLabel.textColor = disabledColor;
        self.timeLabel.textColor = disabledColor;
    }else{
        self.backView.backgroundColor = UIColorWithRGB16Radix(0xFFFAF3);
        self.backView.layer.borderColor = UIColorWithRGB16Radix(0xFFD4A0).CGColor;
        self.titleLabel.textColor = ableColor;
        self.limitLabel.textColor = ableColor;
        self.timeLabel.textColor = ableColor;
    }
}

- (NSMutableAttributedString *)changeToAttributeStringWithSubstringArray:(NSArray *)substringArray
                                                                   Color:(UIColor *)color
                                                                FontSize:(float)fontSize str:(NSString *)str {
    
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:str];
    
    for (NSString *substring in substringArray) {
        
        NSRange range = [str rangeOfString:substring];
        [attributeString addAttribute:NSForegroundColorAttributeName value:color range:range];
        [attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:fontSize] range:range];
    }
    
    return attributeString;
}
@end
